﻿using System.Web;
using System.Xml;
using System;
/// <summary>
///定义工程的全局函数。
/// </summary>
public static class GlobalCls
{
    /// <summary>
    /// adName不带有后缀名.ad
    /// </summary>
    /// <param name="adName"></param>
    public static void GetAd(string adName)
    {
        string adPath = System.Web.HttpContext.Current.Server.MapPath("images/")+adName+".ad";
        if (System.IO.File.Exists(adPath))
        {
            System.Web.HttpContext.Current.Response.WriteFile(adPath);
        }
        else
        {
            string[] imgSize = adName.Split('_');
            //<img src="../images/760_90.jpg" style="width:760px; height:90px" />
            string img = "<img src='images/"+adName+".jpg' style='width:"+imgSize[0]+"px;height:"+imgSize[1]+"px'/>";
            System.Web.HttpContext.Current.Response.Write(img);
        }
    }

    public static string GetClientIP()
    {
        string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
        System.Web.HttpContext.Current.Response.Write(ip);
        return ip;
    }

    public static string GetIpLocation(string ip)
    {
        if (ip=="")
        {
            ip = System.Web.HttpContext.Current.Request.UserHostAddress;
        }
        string url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + ip;
        string stringIpAddress = "";
        ////获取youdao返回的xml格式文件内容
        using (System.Xml.XmlReader read = System.Xml.XmlReader.Create(url))
        {
            while (read.Read())
            {
                switch (read.NodeType)
                {
                    case System.Xml.XmlNodeType.Text://取xml格式文件当中的文本内容
                        if (string.Format("{0}", read.Value).ToString().Trim() != ip)
                        {//youdao返回的xml格式文件内容一个是IP，另一个是IP地址，如果不是IP那么就是IP地址
                            stringIpAddress = string.Format("{0}", read.Value).ToString().Trim();
                        }
                        break;
                }
            }
        }
        HttpContext.Current.Response.Write(stringIpAddress);
        return stringIpAddress;
    }
    public static string GetIpLoc(string ip)
    {
        if (ip == "")
        {
            ip = System.Web.HttpContext.Current.Request.UserHostAddress;
        }
        string url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + ip;
        string stringIpAddress = "";
        ////获取youdao返回的xml格式文件内容
        using (System.Xml.XmlReader read = System.Xml.XmlReader.Create(url))
        {
            while (read.Read())
            {
                switch (read.NodeType)
                {
                    case System.Xml.XmlNodeType.Text://取xml格式文件当中的文本内容
                        if (string.Format("{0}", read.Value).ToString().Trim() != ip)
                        {//youdao返回的xml格式文件内容一个是IP，另一个是IP地址，如果不是IP那么就是IP地址
                            stringIpAddress = string.Format("{0}", read.Value).ToString().Trim();
                        }
                        break;
                }
            }
        }
        return stringIpAddress;
    }

    public static string GetTimeFromServer()
    {
        string time = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        HttpContext.Current.Response.Write(time);
        return time;
    }

    public static void TongJi(string fileName)
    {
        string adPath = System.Web.HttpContext.Current.Server.MapPath("js/") + fileName + ".js";
        if (System.IO.File.Exists(adPath))
        {
            System.Web.HttpContext.Current.Response.WriteFile(adPath);
        }
    }
}
